package com.ruoyi.yipaiju.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yipaiju.domain.ForeclosedHouse;
import com.ruoyi.yipaiju.service.IForeclosedHouseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * fo_foreclosed_houseController
 * 
 * @author liuwp
 * @date 2023-03-01
 */
@RestController
@RequestMapping("/yipaiju/foreclosed_house")
public class ForeclosedHouseController extends BaseController
{
    @Autowired
    private IForeclosedHouseService foreclosedHouseService;

    /**
     * 查询fo_foreclosed_house列表
     */
    @PreAuthorize("@ss.hasPermi('yipaiju:foreclosed_house:list')")
    @GetMapping("/list")
    public TableDataInfo list(ForeclosedHouse foreclosedHouse)
    {
        startPage();
        List<ForeclosedHouse> list = foreclosedHouseService.selectForeclosedHouseList(foreclosedHouse);
        return getDataTable(list);
    }

    /**
     * 导出fo_foreclosed_house列表
     */
    @PreAuthorize("@ss.hasPermi('yipaiju:foreclosed_house:export')")
    @Log(title = "fo_foreclosed_house", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ForeclosedHouse foreclosedHouse)
    {
        List<ForeclosedHouse> list = foreclosedHouseService.selectForeclosedHouseList(foreclosedHouse);
        list.forEach(item -> {
            item.setDiscountRate(item.getDiscountRate().multiply(new BigDecimal("10")));
        });
        ExcelUtil<ForeclosedHouse> util = new ExcelUtil<ForeclosedHouse>(ForeclosedHouse.class);
        util.exportExcel(response, list, "优选房源");
    }

    /**
     * 获取fo_foreclosed_house详细信息
     */
    @PreAuthorize("@ss.hasPermi('yipaiju:foreclosed_house:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(foreclosedHouseService.selectForeclosedHouseById(id));
    }

    /**
     * 新增fo_foreclosed_house
     */
    @PreAuthorize("@ss.hasPermi('yipaiju:foreclosed_house:add')")
    @Log(title = "fo_foreclosed_house", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ForeclosedHouse foreclosedHouse)
    {
        return toAjax(foreclosedHouseService.insertForeclosedHouse(foreclosedHouse));
    }

    /**
     * 修改fo_foreclosed_house
     */
    @PreAuthorize("@ss.hasPermi('yipaiju:foreclosed_house:edit')")
    @Log(title = "fo_foreclosed_house", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ForeclosedHouse foreclosedHouse)
    {
        if (foreclosedHouse.getBuiltUpAreaSquareCentimeter() != null){
            foreclosedHouse.setUnitPriceYuan(new BigDecimal(foreclosedHouse.getPriceYuan()).divide(foreclosedHouse.getBuiltUpAreaSquareCentimeter(), BigDecimal.ROUND_HALF_UP).intValue());
        }
        return toAjax(foreclosedHouseService.updateForeclosedHouse(foreclosedHouse));
    }

    /**
     * 删除fo_foreclosed_house
     */
    @PreAuthorize("@ss.hasPermi('yipaiju:foreclosed_house:remove')")
    @Log(title = "fo_foreclosed_house", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(foreclosedHouseService.deleteForeclosedHouseByIds(ids));
    }

}
